package vn.lumi.smarthome.network;

import java.io.UnsupportedEncodingException;

import vn.lumi.smarthome.io.model.Device;
import vn.lumi.smarthome.io.model.Floor;
import vn.lumi.smarthome.io.model.Room;

public class Config extends HCMsg {

	//constants
	
	
	//Attributes
	private int Floor_id = 0;
//	private int NumOfRoom = 0;
//	private ArrayList<roomConfig> Room = null;
	
	public Config() {
		super(Message.CMD_HC_CONFIG);
//		Room = new ArrayList<roomConfig>();
	}

	@Override
	public void parseMessage(byte[] input) {
//		if(this.isCorrectFormat(input) == true){
//			Floor_id = input[1];
//			numOfRoom = input[2];
//			System.out.println("Floor id = " + Floor_id + ", number of room = " + numOfRoom);
//			int currentPos = 3;			
//			for(int i = 0; i<numOfRoom; i++){
//				roomConfig room_temp = new roomConfig();
//				room_temp.setRoom_id(input[currentPos]);
//				currentPos++;
//				byte[] name = new byte[20];
//				for(int j = 0; j<20; j++){
//					name[j] = input[currentPos];
//					currentPos++;
//				}
//				try {
//					room_temp.setRoomName(new String(name, "UTF-16BE"));
//				} catch (UnsupportedEncodingException e) {
//					e.printStackTrace();
//				}
//				room_temp.setNumOfDevice(input[currentPos]);
//				
//				System.out.println("Room_id = " + room_temp.getRoom_id() + " Name = " + room_temp.getRoomName() + " NumOfDev = " + room_temp.getNumOfDevice());
//				
//				currentPos++;
//				int NumOfDevice = room_temp.getNumOfDevice();
//				for(int j = 0; j<NumOfDevice; j++){
//					deviceConfig dev = new deviceConfig();
//					dev.Device_id = input[currentPos];
//					currentPos++;
//					byte[] dev_name = new byte[20];
//					for(int k = 0; k<20; k++){
//						dev_name[k] = input[currentPos];
//						currentPos++;
//					}
//					try {
//						dev.DeviceName = new String(dev_name, "UTF-16BE");
//					} catch (UnsupportedEncodingException e) {
//						e.printStackTrace();
//					}
//					dev.DeviceType = input[currentPos];
//					currentPos++;
//					int stateLength = deviceStateLength(dev.DeviceType);
//					byte[] state = new byte[stateLength];
//					for(int k = 0; k<stateLength; k++){
//						state[k] = input[currentPos];
//						currentPos++;
//					}
//					dev.DeviceState = state;
//					room_temp.addDevice(dev);
//					
//					System.out.println("Dev id=" + dev.Device_id + " name=" + dev.DeviceName + " type=" + dev.DeviceType + " state=" + Arrays.toString(dev.DeviceState));
//				}
//				this.Room.add(room_temp);
//			}
//		}
		
	}
	public Floor getFloors(byte[] input){
		Floor floor=new Floor();
		floor.setId(input[1]);
		int numOfRoom = input[2];
		Room[] rooms=new Room[numOfRoom];
		System.out.println("Floor id = " + Floor_id + ", number of room = " + numOfRoom);
		int currentPos = 3;			
		for(int i = 0; i<numOfRoom; i++){
			Room room=new Room();
			
//			roomConfig room_temp = new roomConfig();
//			room_temp.setRoom_id(input[currentPos]);
			room.setId(input[currentPos]);
			currentPos++;
			byte[] name = new byte[20];
			for(int j = 0; j<20; j++){
				name[j] = input[currentPos];
				currentPos++;
			}
			try {
//				room_temp.setRoomName(new String(name, "UTF-16BE"));
				room.setName(new String(name, "UTF-16BE"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
//			room_temp.setNumOfDevice(input[currentPos]);
			int numOfDevice=input[currentPos];
			Device[] devices=new Device[numOfDevice];
			System.out.println("Room_id = " + room.getId() + " Name = " + room.getName());
			
			currentPos++;
//			int NumOfDevice = room_temp.getNumOfDevice();
			for(int j = 0; j<numOfDevice; j++){
				Device device=new Device();
//				deviceConfig dev = new deviceConfig();
//				dev.Device_id = input[currentPos];
				device.setId(input[currentPos]);
				currentPos++;
				byte[] dev_name = new byte[20];
				for(int k = 0; k<20; k++){
					dev_name[k] = input[currentPos];
					
					currentPos++;
				}
				try {
					device.setName(new String(dev_name, "UTF-16BE"));
//					dev.DeviceName = new String(dev_name, "UTF-16BE");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
//				dev.DeviceType = input[currentPos];
				device.setType_id(input[currentPos]);
				currentPos++;
//				int stateLength = deviceStateLength(dev.DeviceType);
				int stateLength=deviceStateLength(device.getType_id());
				byte[] state = new byte[stateLength];
				for(int k = 0; k<stateLength; k++){
					state[k] = input[currentPos];
					currentPos++;
				}
//				dev.DeviceState = state;
//				room_temp.addDevice(dev);
				devices[j]=device;
//				System.out.println("Dev id=" + dev.Device_id + " name=" + dev.DeviceName + " type=" + dev.DeviceType + " state=" + Arrays.toString(dev.DeviceState));
			}
			room.setDevices(devices);
			rooms[i]=room;
		}
		floor.setRooms(rooms);
		return floor;
	}
	
//	public class roomConfig{
//		public int Room_id = 0;
//		public String RoomName = null;
//		public int NumOfDevice = 0;
//		ArrayList<deviceConfig> Device = null;
//		
//		public roomConfig(){
//			Room_id = 0;
//			RoomName = null;
//			NumOfDevice = 0;
//			Device = new ArrayList<deviceConfig>();
//		}
//		public int getRoom_id(){
//			return Room_id;			
//		}
//		public void setRoom_id(int room_id){
//			Room_id = room_id;
//		}
//		public String getRoomName(){
//			return RoomName;
//		}
//		public void setRoomName(String name){
//			RoomName = name;
//		}
//		public int getNumOfDevice(){
//			return NumOfDevice;
//		}
//		public void setNumOfDevice(int num){
//			NumOfDevice = num;
//		}
//		public void addDevice(deviceConfig dev){
//			Device.add(dev);
//		}
//		public deviceConfig getDevice(int pos){
//			return Device.get(pos);
//		}
//		
//	}
	
	public class deviceConfig{		
		public int Device_id = 0;
		public String DeviceName = null;
		public int DeviceType = 0;
		public byte[] DeviceState = null;
	}
	
	public int deviceStateLength(int Type){
		int result = 0;
		if(Type == 0x01) result = 1;
		if(Type == 0x02) result = 1;
		if(Type == 0x03) result = 1;
		if(Type == 0x04) result = 1;
		if(Type == 0x05) result = 1;
		if(Type == 0x06) result = 1;
		if(Type == 0x07) result = 3;
		if(Type == 0x08) result = 1;
		if(Type == 0x09) result = 1;
		if(Type == 0x0A) result = 1;
		return result;
		
	}

}
